module.exports = options => {
  return async function jwt(ctx, next) {
    await next();
    const token = ctx.request.header.authorization;
    let decode: any;
    if (token) {
      try {
        // 解码token
        decode = ctx.app.jwt.verify(token, options.secret);
        console.log('解码结果是');
        console.log(decode);
        const access_token = await ctx.app.redis.get(`${decode.uid}_access_token`) as string;
        const refresh_token = await ctx.app.redis.get(`${decode.uid}_refresh_token`);
        console.log('access_token是' + access_token);
        console.log('refresh_token是' + refresh_token);
        if (decode.refresh_token !== refresh_token || decode.access_token !== access_token) {
          ctx.status = 401;
          if (!refresh_token) {
            ctx.body = {
              err_msg: 'refresh_token过期',
            };
          } else {
            ctx.body = {
              err_msg: 'access_token过期',
            };
          }
        }
      } catch (error) {
        ctx.status = 401;
        ctx.body = {
          err_msg: 'token无效',
        };
      }
    } else {
      ctx.status = 401;
      ctx.body = {
        err_msg: '没有token',
      };
    }
  };
};
